home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / userintf / smplbtn.sx < prev   
Encoding:
Text File  |  1996-05-21  |  2.6 KB  |  115 lines  |  [TEXT/ttxt]

  1. --<<<
  2. -- first create a window
  3. object myWindow (Window)
  4.     boundary:(new Rect x2:200 y2:200)
  5.     settings x:300, y:50
  6. end
  7. show myWindow
  8.  
  9. -- create a space to control actuators in the window
  10. object myActuatorController (ActuatorController)
  11.     enabled:true, space:myWindow, wholeSpace:true
  12. end
  13.  
  14. @down
  15. @up -- declare name literals first, good programming practice
  16.  
  17. -- create the SimpleButton class
  18. class SimpleButton (TwoDShape, Actuator)
  19.     instance variables
  20.         authorData
  21.         activateAction
  22.         pressAction
  23.         releaseAction
  24.         currentState -- either @up or @down
  25.     instance methods
  26.  
  27.         method init self #rest args -> (
  28.             apply nextMethod self args
  29.  
  30.             self.currentState := @up
  31.         )
  32. end
  33.  
  34. -- the actuator controller calls activate, press, and release
  35. -- on this button as it receives mouse events.  Specialize these
  36. -- three methods, which are defined by Actuator
  37. -- could also specialize multiActivate here
  38. method activate self {class SimpleButton} -> (
  39.  
  40.     nextmethod self
  41.  
  42.     if self.enabled == false then (
  43.         return self
  44.     ) else (
  45.         self.currentState := @up
  46.         self.fill := whiteBrush
  47.         handleActivate self
  48.         return self
  49.     )
  50. )
  51. method press self {class SimpleButton} -> (
  52.  
  53.     nextmethod self
  54.  
  55.     if self.enabled == false then (
  56.         return self
  57.     ) else (
  58.         self.currentState := @down
  59.         self.fill := blackBrush
  60.         handlePress self
  61.         return self
  62.     )
  63. )
  64. method release self {class SimpleButton} -> (
  65.  
  66.     nextmethod self
  67.  
  68.     if self.enabled == false then (
  69.         return self
  70.     ) else (
  71.         self.currentState := @up
  72.         self.fill := whiteBrush
  73.         handleRelease self
  74.         return self
  75.     )
  76. )
  77.  
  78. -- although it is not required, the activate, press, and release
  79. -- methods are factored just like in PushButton and Toggle
  80. method handleActivate self {class SimpleButton} -> (
  81.     if self.activateAction <> undefined do (
  82.         self.authorData := "activate"
  83.         (self.activateAction) (self.authorData) self
  84.     )
  85.     return self
  86. )
  87. method handlePress self {class SimpleButton} -> (
  88.     if self.pressAction <> undefined do (
  89.         self.authorData := "press"
  90.         (self.pressAction) (self.authorData) self
  91.     )
  92.     return self
  93. )
  94. method handleRelease self {class SimpleButton} -> (
  95.     if self.releaseAction <> undefined do (
  96.         self.authorData := "release"
  97.         (self.releaseAction) (self.authorData) self
  98.     )
  99.     return self
  100. )
  101.  
  102. -- now create an instance of SimpleButton and append it to the window
  103. object myButton (SimpleButton)
  104.     boundary:(New Oval x2:100 y2:100), stroke:blackBrush
  105.     settings x:50,y:50
  106. end
  107. append myWindow myButton
  108.  
  109. -- set up a function for activateAction, pressAction, and releaseAction
  110. function beep x y -> format debug "beep %*\n" x @normal
  111. myButton.activateAction := beep
  112. myButton.pressAction := beep
  113. myButton.releaseAction := beep
  114. -->>>
  115.